package com.seanLab.service;

import com.seanLab.domain.*;
import com.seanLab.dto.*;
import com.seanLab.repository.EditorSessionRepo;
import com.seanLab.repository.ImageRepo;
import com.seanLab.tool.TagSuggestModel.StaticModelProperties;
import com.seanLab.tool.TagSuggestor;
import com.seanLab.tool.es.ESImageSearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by ailly on 17-9-21.
 */
@Service
public class RecommendService {
    @Autowired
    private ImageRepo imageRepo;

    @Autowired
    private EditorSessionRepo editorSessionRepo;

    @Autowired
    private ESImageSearch esImageSearch;

    public Body recommendImages(RecommendArticleDto recommendArticleDto){
        RecommendImageListDto recommendImageListDto = new RecommendImageListDto();
        String sessionID = "sessionID-" + UUID.randomUUID().toString();
        recommendImageListDto.setSessionID(sessionID);
        //TODO 业务逻辑：抽取article关键词 -> 查询ES返回imglist -> 并修改Dto

        //TODO @吴小波: 抽取article关键词

        //TODO @吴小波 TagSuggestor应返回该无图文章的关键词列表(包含每个keyword的分数). e.g., List<SuggestArticleKeywordsDto> keywords
        //TODO @吴小波 填充以下代码
        //TODO TagSuggestor t1 = new TagSuggestor();
        //TODO List<SuggestArticleKeywordsDto> keywords = t1.doSuggest(recommendArticleDto);

        //FIXME 吴晓波代码实现部分（not reviewed）
        TagSuggestor ts = new TagSuggestor();
        ts.loadModel(StaticModelProperties.ModelPath);
        List<SuggestArticleKeywordsDto> keywords = ts.doSuggest(recommendArticleDto);

        //TODO @张帆: 根据keywords从ES查询imagelist
        //TODO ImageTagService(最好换个名字 e.g., ESImageSearch)
        //TODO ESImageSearch 应根据keywords列表，返回 List<RecommendImageDto> 对象
        //TODO @张帆: 填充以下代码
        //TODO ESImageSearch esImageSearch = new ESImageSearch();
        //TODO List<RecommendImageDto> recommendImgList = esImageSearch.searchByKeyWords(keywords);
        //TODO recommendImageListDto.setImgList(recommendImgList);


        //FIXME 张帆部分review
        ArrayList stumpkeywords=new ArrayList();
        for (SuggestArticleKeywordsDto k : keywords) {
            stumpkeywords.add(k.getKeywords());
        }

        ESImageSearch esImageSearch = this.esImageSearch;
        esImageSearch.initConnection();
        List<RecommendImageDto> recommendImageList=esImageSearch.searchByKeywords(stumpkeywords);
        recommendImageListDto.setImgList(recommendImageList);


        //TODO 推荐之后应 持久化editorSession表（采纳了推荐了哪些图片 id）
        EditorSession editorSession = new EditorSession();
        editorSession.setSessionID(sessionID);
        editorSession.setContent(recommendArticleDto.getContent());
        editorSession.setKeywords(keywords);
        editorSession.setTitle(recommendArticleDto.getTitle());
        editorSession.setUrl(recommendArticleDto.getUrl());
        editorSession.setRecommendDate(new Date());

        editorSession.initInnerImg();

        List<String> tmpSessionRecImgs = new ArrayList<>();
        for (RecommendImageDto i : recommendImageListDto.getImgList()) {
            tmpSessionRecImgs.add(i.getImgID());

            //new setter
            List<String> ids = new ArrayList<>();
            for (RecommendTagInfoDto rt : i.getImgTagList() ) {
                ids.add(rt.getTagID());
            }

            List<String> names = new ArrayList<>();
            for (RecommendTagInfoDto rt : i.getImgTagList() ) {
                names.add(rt.getTagName());
            }
            //new setter
            editorSession.addInnerImg(i.getImgID(), ids, names);
        }
        editorSession.setRecommendedImgs(tmpSessionRecImgs);

        this.editorSessionRepo.save(editorSession);

        Body body = Body.getResponseBody(0, recommendImageListDto);

        return body;
    }

}
